**************************************************************************
*This file builds vars on migration flows of students from the Census/ACS*
**************************************************************************

use "$list/czones_list.dta", clear
expand 2
gen year=1990
bys czone: replace year=2000 if _n==2

*Inmigrants
merge m:1 czone using "$clean_data_lmarket/czone1990_MigrationFlows.dta", assert(3) nogenerate
merge m:1 czone using "$clean_data_lmarket/czone2000_MigrationFlows.dta", assert(3) nogenerate
merge m:1 czone using "$clean_data_lmarket/czone2008_MigrationFlows.dta", assert(3) nogenerate

rename (*_migrant_*) (*_inmigrant_*)

*Outmigrants
merge m:1 czone using "$clean_data_lmarket/czone1990_OutMigrationStudentsOtherCZ.dta", assert(3) nogenerate
merge m:1 czone using "$clean_data_lmarket/czone2000_OutMigrationStudentsOtherCZ.dta", assert(3) nogenerate
merge m:1 czone using "$clean_data_lmarket/czone2008_OutMigrationStudentsOtherCZ.dta", assert(3) nogenerate

rename (ipums_migrant_a19_64_hisp_* ipums_migrant_a19_64_cuba_*) (ipums_migrant_a19_64_hi_* ipums_migrant_a19_64_cu_*)
rename (*_migrant_*) (*_outmigrant_*)

*Normalized (to 1 year) from Census
foreach year in 1990 2000 {

*Inmigrants and stayers)
gen allinStudentsN4_`year' = (ipums_pop_sc_a19_64_`year')/(ipums_pop_a19_64_`year')
gen allinStudentsN5_`year' = (ipums_pop_sc_a19_64_`year')/(ipums_pop_a19_64_`year')

*Stayers (not inmigrants)
gen noninMigrantStudentsN4_`year' = (ipums_pop_sc_a19_64_`year'-ipums_inmigrant_sc_a19_64_`year'/4)/(ipums_pop_a19_64_`year')
gen noninMigrantStudentsN5_`year' = (ipums_pop_sc_a19_64_`year'-ipums_inmigrant_sc_a19_64_`year'/5)/(ipums_pop_a19_64_`year')

*Outmigrants and stayers)
gen alloutStudentsN4_`year' = (ipums_pop_out_sc_a19_64_`year'+ipums_outmigrant_sc_a19_64_`year'/4)/(ipums_pop_out_a19_64_`year')
gen alloutStudentsN5_`year' = (ipums_pop_out_sc_a19_64_`year'+ipums_outmigrant_sc_a19_64_`year'/5)/(ipums_pop_out_a19_64_`year')

*Stayers
gen nonoutMigrantStudentsN4_`year' = (ipums_pop_out_sc_a19_64_`year')/(ipums_pop_out_a19_64_`year')
gen nonoutMigrantStudentsN5_`year' = (ipums_pop_out_sc_a19_64_`year')/(ipums_pop_out_a19_64_`year')

*Set to 0 if adjustment leads to negative values
replace allinStudentsN4_`year' = 0 if allinStudentsN4_`year'<0
replace allinStudentsN4_`year' = 0 if allinStudentsN4_`year'<0

replace noninMigrantStudentsN4_`year' = 0 if noninMigrantStudentsN4_`year'<0
replace noninMigrantStudentsN5_`year' = 0 if noninMigrantStudentsN5_`year'<0

*Inmigrants
gen inMigrantStudentsN4_`year' = (ipums_inmigrant_sc_a19_64_`year'/4)/(ipums_pop_a19_64_`year')
gen inMigrantStudentsN5_`year' = (ipums_inmigrant_sc_a19_64_`year'/5)/(ipums_pop_a19_64_`year')

*Outmigrants
gen outMigrantStudentsN4_`year' = (ipums_outmigrant_sc_a19_64_`year'/4)/(ipums_pop_out_a19_64_`year')
gen outMigrantStudentsN5_`year' = (ipums_outmigrant_sc_a19_64_`year'/5)/(ipums_pop_out_a19_64_`year')

}

*Already 1-year migrants from ACS
foreach year in 2008 {
gen allinStudents_`year' = (ipums_pop_sc_a19_64_`year')/(ipums_pop_a19_64_`year')
gen noninMigrantStudents_`year' = (ipums_pop_sc_a19_64_`year'-ipums_inmigrant_sc_a19_64_`year')/(ipums_pop_a19_64_`year')

gen alloutStudents_`year' = (ipums_pop_out_sc_a19_64_`year'+ipums_outmigrant_sc_a19_64_`year')/(ipums_pop_out_a19_64_`year')
gen nonoutMigrantStudents_`year' = (ipums_pop_out_sc_a19_64_`year')/(ipums_pop_out_a19_64_`year')

gen inMigrantStudents_`year' = (ipums_inmigrant_sc_a19_64_`year')/(ipums_pop_a19_64_`year')
gen outMigrantStudents_`year' = (ipums_outmigrant_sc_a19_64_`year')/(ipums_pop_out_a19_64_`year')

}

*Changes
foreach type in in out {
foreach var in N4 N5 {

gen d_all`type'Students`var' = 0.7*100*(all`type'Students`var'_2000-all`type'Students`var'_1990) if year == 1990
replace d_all`type'Students`var' = 100*(all`type'Students_2008-all`type'Students`var'_2000) if year == 2000

gen d_non`type'MigrantStudents`var' = 0.7*100*(non`type'MigrantStudents`var'_2000-non`type'MigrantStudents`var'_1990) if year == 1990
replace d_non`type'MigrantStudents`var' = 100*(non`type'MigrantStudents_2008-non`type'MigrantStudents`var'_2000) if year == 2000

gen d_`type'MigrantStudents`var' = 0.7*100*(`type'MigrantStudents`var'_2000-`type'MigrantStudents`var'_1990) if year == 1990
replace d_`type'MigrantStudents`var' = 100*(`type'MigrantStudents_2008-`type'MigrantStudents`var'_2000) if year == 2000

}
}

save "$final_data_outcomes/czone_StudentMigrantsInOut.dta", replace
